#include <iostream>
#include "connectPool.h"
#include <chrono>
#include "mysqlDataBase.h"

using namespace std;

void op1()
{
    const char *host = "localhost";
    const char *user = "root";
    const char *passwd = "1016";
    for (int idx = 0; idx < 5000; idx++)
    {
        // 连接数据库
        MySqlDataBase conn(host, user, passwd);
        conn.connectDB("musicDB");

        const char *sql = "insert into userInfo (username, age) values ('zhangsan', 20);";
        
        // 执行sql语句
        conn.executeSql(sql);

        // 关闭数据库
        conn.close();
    }
}

void op2(ConnectPool *pool)
{
    for (int idx = 0; idx < 5000; idx++)
    {
        std::shared_ptr<MySqlDataBase> conn = pool->getConnection();

        const char *sql = "insert into userInfo (username, age) values ('zhangsan', 20);";

        conn->executeSql(sql);
    }
}


void test()
{
#if 0
    // 不使用连接池
    std::chrono::_V2::steady_clock::time_point startTime = std::chrono::_V2::steady_clock::now();
    op1();
    std::chrono::_V2::steady_clock::time_point endTime = std::chrono::_V2::steady_clock::now();
    
    auto interval = endTime - startTime;
    cout << "interval:" << interval.count() << endl;
#endif

#if 1
    // 使用连接池
    ConnectPool *pool = ConnectPool::getConnectPool();

    std::chrono::_V2::steady_clock::time_point startTime = std::chrono::_V2::steady_clock::now();
    op2(pool);
    std::chrono::_V2::steady_clock::time_point endTime = std::chrono::_V2::steady_clock::now();
    
    auto interval = endTime - startTime;
    cout << "interval:" << interval.count() << endl;
#endif
}

int main()
{
    test();

    return 0;
}